home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ExString.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  3KB  |  120 lines

  1. // ExString.c -- Dynamic character strings
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ExString.c,v 3.0 90/05/15 22:43:34 kgorlen Rel $
  4.  
  5. #include "ExString.h"
  6. #include <malloc.h>
  7.  
  8. inline unsigned MAX(unsigned a, unsigned b)
  9.      { return a >= b ? a : b; }
  10.  
  11. String::String(unsigned extra)
  12. // Construct an empty String
  13. {
  14.     len = 0;
  15.     p = new char[alloc = extra+1];
  16.     *p = '\0';
  17. }
  18.  
  19. String::String(const char* cs, unsigned extra)
  20. // Construct a String from a C string
  21.     len = strlen(cs);
  22.     p = new char[alloc = len+extra+1];
  23.     strcpy (p,cs); 
  24. }
  25.  
  26. String::String(const String& s)
  27. // Construct one String from another
  28. {
  29.     len = s.len;
  30.     p = new char[alloc = s.alloc];
  31.     strcpy (p,s.p);
  32. }
  33.  
  34. String::String(const SubString& ss)
  35. // Construct a String from a SubString
  36. {
  37.     len = ss.sl;
  38.     p = new char[alloc = len + 1];
  39.     strncpy(p,ss.sp,len);
  40.     p[len] = '\0';
  41. }
  42.  
  43. SubString String::operator()(unsigned pos, unsigned lgt)
  44. // Extract a SubString from a String
  45. {   
  46.     return SubString(*this, pos, lgt);
  47. }
  48.  
  49. const SubString String::operator()(unsigned pos,
  50.                                    unsigned lgt) const
  51. // Extract a SubString from a String
  52. {   
  53.     return SubString(*this, pos, lgt);
  54. }
  55.  
  56. void String::operator=(const String& s)
  57. // Assign one String to another
  58. {
  59.     if (p == s.p) return;
  60.     len = s.len;
  61.     if (len >= alloc) {
  62.         delete p;
  63.         p = new char[alloc = s.alloc];
  64.     }
  65.     strcpy (p,s.p);
  66. }
  67.  
  68. void String::operator&=(const String& s)
  69. // Concatenate a String with another
  70. {
  71.     if (alloc <= len + s.len) {
  72.         alloc += s.len;
  73.         p = realloc(p, alloc);  // this is slightly dangerous!
  74.     }
  75.     strcpy (&p[len],s.p);
  76.     len += s.len;
  77. }   
  78.  
  79. void String::printOn(ostream& strm) const
  80. // Print this String on an ostream
  81. {
  82.     strm << p;
  83. }
  84.  
  85. void String::scanFrom(istream& strm)
  86. // Read next line of input from strm into this String.
  87. {
  88.     char c;
  89.     strm.get(c);
  90.     if (c != '\n') strm.putback(c);
  91.     char temp[513];
  92.     strm.get(temp,513);
  93.     *this = String(temp);
  94. }
  95.  
  96. ostream& operator<<(ostream& strm, const String& s)
  97. // Print String s on ostream strm
  98. {
  99.     s.printOn(strm);
  100.     return strm;
  101. }
  102.  
  103. istream& operator>>(istream& strm, String& s)
  104. // Read String s from istream strm
  105. {
  106.     s.scanFrom(strm);
  107.     return strm;
  108. }
  109.  
  110. String SubString::operator&(const String& s) const
  111. // Concatenate a SubString and a String
  112. {
  113.     String t(sl + s.alloc - 1);
  114.     strncpy(t.p, sp, sl);
  115.     strcpy(&(t.p[sl]), s.p);
  116.     t.len = sl + s.len;
  117.     return t;
  118. }
  119.